package io.xccit.frame;

import io.xccit.pojo.User;
import io.xccit.service.UserService;
import io.xccit.service.impl.UserServiceImpl;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author China_ywx
 */
public class LoginFrame extends JFrame {

    private UserService userService = new UserServiceImpl();
    public LoginFrame() {
        initComponents();
    }

    /**
     * 设置登录参数
     * @param username
     * @param password
     */
    public void setValue(String username, String password) {
        this.username_input.setText(username);
        this.password_input.setText(password);
    }

    /**
     * 登录按钮鼠标单击
     * @param e
     */
    private void login_btnMouseClicked(MouseEvent e) {
        String username = username_input.getText().toString();
        String password = String.valueOf(password_input.getPassword());
        User user = new User(username, password);
        System.out.println(user);
        User login = userService.login(user);
        if (login != null){
            JOptionPane.showMessageDialog(null,"登录成功");
        }else {
            JOptionPane.showMessageDialog(null,"登录失败");
        }
    }

    /**
     * 注册按钮鼠标单击
     * @param e
     */
    private void register_btnMouseClicked(MouseEvent e) {
        dispose();
        RegisterFrame registerFrame = new RegisterFrame();
        registerFrame.setVisible(true);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        title = new JLabel();
        username_label = new JLabel();
        username_input = new JTextField();
        password_label = new JLabel();
        password_input = new JPasswordField();
        login_btn = new JButton();
        register_btn = new JButton();
        login_img = new JLabel();

        //======== this ========
        setBackground(new Color(0x66ffcc));
        setTitle("\u5b66\u751f\u8ba2\u9910\u7cfb\u7edf-\u767b\u5f55");
        setIconImage(new ImageIcon(getClass().getResource("/images/globalIcon.png")).getImage());
        setResizable(false);
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- title ----
        title.setText("\u5b66\u751f\u7f51\u4e0a\u8ba2\u9910\u7cfb\u7edf-\u767b\u5f55");
        title.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 22));
        contentPane.add(title);
        title.setBounds(400, 85, 230, 30);

        //---- username_label ----
        username_label.setText("username:");
        username_label.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 14));
        contentPane.add(username_label);
        username_label.setBounds(305, 170, 70, 20);

        //---- username_input ----
        username_input.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(username_input);
        username_input.setBounds(390, 165, 260, 30);

        //---- password_label ----
        password_label.setText("password:");
        password_label.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 14));
        contentPane.add(password_label);
        password_label.setBounds(305, 235, 70, 20);
        contentPane.add(password_input);
        password_input.setBounds(390, 230, 260, 30);

        //---- login_btn ----
        login_btn.setText("\u767b \u5f55");
        login_btn.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        login_btn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                login_btnMouseClicked(e);
            }
        });
        contentPane.add(login_btn);
        login_btn.setBounds(395, 290, 73, 25);

        //---- register_btn ----
        register_btn.setText("\u6ce8 \u518c");
        register_btn.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        register_btn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                register_btnMouseClicked(e);
            }
        });
        contentPane.add(register_btn);
        register_btn.setBounds(565, 290, 73, 25);

        //---- login_img ----
        login_img.setIcon(new ImageIcon(getClass().getResource("/images/login_img.png")));
        login_img.setHorizontalAlignment(SwingConstants.CENTER);
        login_img.setToolTipText("\u6b66\u5a01\u804c\u4e1a\u5b66\u9662\u6821\u56ed\u8ba2\u9910");
        contentPane.add(login_img);
        login_img.setBounds(0, 0, 296, 458);

        contentPane.setPreferredSize(new Dimension(705, 490));
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel title;
    private JLabel username_label;
    private JTextField username_input;
    private JLabel password_label;
    private JPasswordField password_input;
    private JButton login_btn;
    private JButton register_btn;
    private JLabel login_img;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
